﻿using MediatR;
using SystemLYH.API.Application.Command.LocationCommand;
using SystemLYH.Domain.Dto;
using SystemLYH.Domain;
using SystemLYH.Infrstructre.Interface;

namespace SystemLYH.API.Application.Handlers.LocationHandlers
{
    public class ParticularsShowLocationCommandHandler:IRequestHandler<ParticularsShowLocationCommand, PageDTO<Location>>
    {
        /// <summary>
        /// 库区
        /// </summary>
        protected readonly IReservoirRepository _reservoirRepository;
        /// <summary>
        /// 库位
        /// </summary>
        protected readonly ILocationRepository _locationRepository;
        /// <summary>
        /// 库区构造函数
        /// </summary>
        /// <param name="reservoirRepository"></param>
        public ParticularsShowLocationCommandHandler(IReservoirRepository reservoirRepository, ILocationRepository locationRepository)
        {
            _reservoirRepository = reservoirRepository;
            _locationRepository = locationRepository;
        }

        public async Task<PageDTO<Location>> Handle(ParticularsShowLocationCommand request, CancellationToken cancellationToken)
        {
            PageDTO<Location> page = new PageDTO<Location>();
            var Repositorylist = (await _reservoirRepository.GetAllAsync()).Where(x => !x.IsDelReservoir&x.WarehouseID==request.WarehouseID);
            var list = (from res in Repositorylist
                        join loc in await _locationRepository.GetAllAsync()
                        on res.ReservoirName equals loc.ReservoirId
                        select loc);
            page.pageRoot = list.Count();
            page.pageCount = (int)Math.Ceiling(page.pageRoot * 1.0 / request.pageSize);
            page.plist = list.OrderByDescending(x => x.ReservoirId).Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();
            return page;
        }
    }
}
